package commons;


public class UUIDUtils {
    /**
     * Very high performance (unwrapped loop ; checks on characters) test class
     * to check the validity of an uuid.
     * <p>
     * Warning, this method is case sensitive.
     * 
     * @param uuid
     * @return <code>true</code> if the uuid string is a real uuid,
     *         <code>false</code> otherwise
     */
    public static boolean isValid(String uuid) {
        if (uuid == null) {
            return false;
        }
        if (uuid.length() != 36) {
            return false;
        }
        // We could have put this in a loop, but the best optimizing technique
        // is the loop unwrapping ;)
        
        char[] chars = uuid.toCharArray();
        
        // First 
        char c = chars[0];
        if ((c < '0' || c > '9') && (c < 'a' || c > 'f')) {
            return false;
        }
        c = chars[1];
        if ((c < '0' || c > '9') && (c < 'a' || c > 'f')) {
            return false;
        }
        c = chars[2];
        if ((c < '0' || c > '9') && (c < 'a' || c > 'f')) {
            return false;
        }
        c = chars[3];
        if ((c < '0' || c > '9') && (c < 'a' || c > 'f')) {
            return false;
        }
        c = chars[4];
        if ((c < '0' || c > '9') && (c < 'a' || c > 'f')) {
            return false;
        }
        c = chars[5];
        if ((c < '0' || c > '9') && (c < 'a' || c > 'f')) {
            return false;
        }
        c = chars[6];
        if ((c < '0' || c > '9') && (c < 'a' || c > 'f')) {
            return false;
        }
        c = chars[7];
        if ((c < '0' || c > '9') && (c < 'a' || c > 'f')) {
            return false;
        }
        if (chars[8] != '-') {
            return false;
        }

        c = chars[9];
        if ((c < '0' || c > '9') && (c < 'a' || c > 'f')) {
            return false;
        }
        c = chars[10];
        if ((c < '0' || c > '9') && (c < 'a' || c > 'f')) {
            return false;
        }
        c = chars[11];
        if ((c < '0' || c > '9') && (c < 'a' || c > 'f')) {
            return false;
        }
        c = chars[12];
        if ((c < '0' || c > '9') && (c < 'a' || c > 'f')) {
            return false;
        }
        if (chars[13] != '-') {
            return false;
        }

        c = chars[14];
        if ((c < '0' || c > '9') && (c < 'a' || c > 'f')) {
            return false;
        }
        c = chars[15];
        if ((c < '0' || c > '9') && (c < 'a' || c > 'f')) {
            return false;
        }
        c = chars[16];
        if ((c < '0' || c > '9') && (c < 'a' || c > 'f')) {
            return false;
        }
        c = chars[17];
        if ((c < '0' || c > '9') && (c < 'a' || c > 'f')) {
            return false;
        }
        if (chars[18] != '-') {
            return false;
        }

        c = chars[19];
        if ((c < '0' || c > '9') && (c < 'a' || c > 'f')) {
            return false;
        }
        c = chars[20];
        if ((c < '0' || c > '9') && (c < 'a' || c > 'f')) {
            return false;
        }
        c = chars[21];
        if ((c < '0' || c > '9') && (c < 'a' || c > 'f')) {
            return false;
        }
        c = chars[22];
        if ((c < '0' || c > '9') && (c < 'a' || c > 'f')) {
            return false;
        }
        if (chars[23] != '-') {
            return false;
        }

        c = chars[24];
        if ((c < '0' || c > '9') && (c < 'a' || c > 'f')) {
            return false;
        }
        c = chars[25];
        if ((c < '0' || c > '9') && (c < 'a' || c > 'f')) {
            return false;
        }
        c = chars[26];
        if ((c < '0' || c > '9') && (c < 'a' || c > 'f')) {
            return false;
        }
        c = chars[27];
        if ((c < '0' || c > '9') && (c < 'a' || c > 'f')) {
            return false;
        }
        c = chars[28];
        if ((c < '0' || c > '9') && (c < 'a' || c > 'f')) {
            return false;
        }
        c = chars[29];
        if ((c < '0' || c > '9') && (c < 'a' || c > 'f')) {
            return false;
        }
        c = chars[30];
        if ((c < '0' || c > '9') && (c < 'a' || c > 'f')) {
            return false;
        }
        c = chars[31];
        if ((c < '0' || c > '9') && (c < 'a' || c > 'f')) {
            return false;
        }
        c = chars[32];
        if ((c < '0' || c > '9') && (c < 'a' || c > 'f')) {
            return false;
        }
        c = chars[33];
        if ((c < '0' || c > '9') && (c < 'a' || c > 'f')) {
            return false;
        }
        c = chars[34];
        if ((c < '0' || c > '9') && (c < 'a' || c > 'f')) {
            return false;
        }
        c = chars[35];
        if ((c < '0' || c > '9') && (c < 'a' || c > 'f')) {
            return false;
        }

        return true;
    }

}
